/*
		A Calendar module for the desktop2 system, displayed in a 7 X 6 Grid
		
		Author: 	Matthew Allpress
		Created:	15/07/06
		Edited:		24/09/06

*/

var cal_mod_name = "Calendar";

var CalendarBackend = "http://localhost:5050/todo/index.php?mid=4";

var toAdd = "";
toAdd+="<div style=\"position: absolute; overflow: hidden; overflow-x : hidden; width: 175px; height: 175px; z-index: 1; right: 0px; top: 590px; background:#FFFFFF;\" id=\"cal_main\">";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 0px\" id=\"cal_Mon\" align=\"center\">M<\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 0px\" id=\"cal_Tue\" align=\"center\">T<\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 0px\" id=\"cal_Wed\" align=\"center\">W<\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 0px\" id=\"cal_Thur\" align=\"center\">T<\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 0px\" id=\"cal_Fri\" align=\"center\">F<\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 0px\" id=\"cal_Sat\" align=\"center\">S<\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 0px\" id=\"cal_Sun\" align=\"center\">S<\/div>";

toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 25px\" id=\"cal_0.0\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 25px\" id=\"cal_1.0\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 25px\" id=\"cal_2.0\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 25px\" id=\"cal_3.0\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 25px\" id=\"cal_4.0\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 25px\" id=\"cal_5.0\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 25px\" id=\"cal_6.0\"><\/div>";

toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 50px\" id=\"cal_0.1\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 50px\" id=\"cal_1.1\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 50px\" id=\"cal_2.1\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 50px\" id=\"cal_3.1\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 50px\" id=\"cal_4.1\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 50px\" id=\"cal_5.1\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 50px\" id=\"cal_6.1\"><\/div>";
	
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 75px\" id=\"cal_0.2\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 75px\" id=\"cal_1.2\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 75px\" id=\"cal_2.2\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 75px\" id=\"cal_3.2\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 75px\" id=\"cal_4.2\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 75px\" id=\"cal_5.2\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 75px\" id=\"cal_6.2\"><\/div>";


toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 100px\" id=\"cal_0.3\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 100px\" id=\"cal_1.3\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 100px\" id=\"cal_2.3\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 100px\" id=\"cal_3.3\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 100px\" id=\"cal_4.3\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 100px\" id=\"cal_5.3\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 100px\" id=\"cal_6.3\"><\/div>";

toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 125px\" id=\"cal_0.4\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 125px\" id=\"cal_1.4\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 125px\" id=\"cal_2.4\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 125px\" id=\"cal_3.4\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 125px\" id=\"cal_4.4\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 125px\" id=\"cal_5.4\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 125px\" id=\"cal_6.4\"><\/div>";

toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 0px; top: 150px\" id=\"cal_0.5\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 25px; top: 150px\" id=\"cal_1.5\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 50px; top: 150px\" id=\"cal_2.5\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 75px; top: 150px\" id=\"cal_3.5\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 100px; top: 150px\" id=\"cal_4.5\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 125px; top: 150px\" id=\"cal_5.5\"><\/div>";
toAdd+="<div style=\"border-style:solid; border-width:1px;position: absolute; width: 25px; height: 25px; z-index: 1; left: 150px; top: 150px\" id=\"cal_6.5\"><\/div>";

toAdd+="<\/div>";

toAdd+="<div style=\"position: absolute; width: 350px; height: 170px; z-index: 2; left: 0px; top: 0px; visibility:hidden\" id=\"calItemPopupMain\">";
toAdd+="<div style=\"position: absolute; background:#FFFFFF; width: 350px; height: 24px; z-index: 3; left: 0px; top: 0px\" id=\"calItemPopupBar\">";
toAdd+="<div style=\"position: absolute; background:#FFFFFF; width: 16; height: 16px; z-index: 3; right: 2px; top: 2px\" id=\"calItemPopupClose\" >";
toAdd+="<a onclick=\"document.getElementById('calItemPopupMain').style.visibility='hidden';\" style=\"font-size:12\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>";
toAdd+="<\/div>";
toAdd+="<div style=\"position: absolute; background:#FFFFFF; width: 350; height: 146px; z-index: 3; left: 0px; top: 20px\" id=\"calItemPopupText\">";
toAdd+="<\/div>";
toAdd+="<\/div>";
toAdd+="<\/div>";


document.body.innerHTML+=toAdd;

var temp_element = document.getElementById("cal_main");

if(module_config[cal_mod_name]['left'] != 'N')
{ 
	temp_element.style.left = module_config[cal_mod_name]['left']+"px";
}

if(module_config[cal_mod_name]['right'] != 'N')
{ 
	temp_element.style.right = module_config[cal_mod_name]['right']+"px";
}
if(module_config[cal_mod_name]['top'] != 'N')
{
	temp_element.style.top = module_config[cal_mod_name]['top']+"px";
}

temp_element = null;
document.getElementById("calItemPopupBar").style.backgroundColor = module_config[cal_mod_name]['pop_head']
document.getElementById("calItemPopupText").style.backgroundColor = module_config[cal_mod_name]['pop_bg']

toAdd = null;

var currentDate = new Date();
var calPopupXpos = 0;
var calPopupYpos = 0;

window.setInterval("callUpdateCalendar();", 120000);

callUpdateCalendar();

function callUpdateCalendar()
{
	makeNewRequest("GET", CalendarBackend, CalendarReqChange);
}

function callCreatePopup(objToSet, date)
{
	calPopupXpos = getAbsoluteOffsetLeft(objToSet) -350;//- objToSet.style.width;
	calPopupYpos = getAbsoluteOffsetTop(objToSet) +25;
	makeNewRequest("GET", CalendarBackend + "&do=get_items&date=" + date, CalendarItemReqChange);
}

function CalendarItemReqChange() 
{
 // If data received correctly
 if (XMLRequestObject.readyState == 4) 
 {
  // if data is valid
  if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
  { 	
   // Parsing RSS
   try
   {
	   var node = XMLRequestObject.responseXML.documentElement; 
	   // Get Channel information
	   var items = node.getElementsByTagName('item');

	   var time = null;
	   var title = null;
	   var temp_popup = document.getElementById("calItemPopupMain");
	   var temp_popup_text = document.getElementById("calItemPopupText");
	   temp_popup_text.innerHTML=""
	   for (var i=0; i < items.length; i++)
	   {
			time = items[i].getAttribute("time");
			title = items[i].getAttribute("title");
			temp_popup_text.innerHTML+=title+ ": " + time + "<br>";
	   }
	   temp_popup.style.visibility="visible";
	   temp_popup.style.left=calPopupXpos;
	   temp_popup.style.top=calPopupYpos;
	   day = null;
	   time = null;
	   items = null;
	   node = null;
	}
	catch(err)
	{
		//add_to_console("Error returned when calling calendar update, retrying");
		//callCreatePopup();
	}
  }
  else 
  {
   // Tell the reader that there was error requesting data
   alert("ERRRROR: YOU BROKE THE INTERWEB");
  }
 }
}

function CalendarReqChange() 
{
 // If data received correctly
 if (XMLRequestObject.readyState == 4) 
 {
  //alert(XMLRequestObject.responseText);
  // if data is valid
  if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
  { 	
   // Parsing RSS
   try
   {
	   var node = XMLRequestObject.responseXML.documentElement; 
	   // Get Channel information
	   var dates = node.getElementsByTagName('day');
	   var month = node.getElementsByTagName('month');
	   var current_year = node.getAttribute("y");
	   var current_month = node.getAttribute("d");
	   for (var i=0; i < dates.length; i++)
	   {
			day = dates[i].getAttribute("d");
			col = dates[i].getAttribute("c");
			row = dates[i].getAttribute("r");
			events = dates[i].getAttribute("e");
			if(events > 0)
			{
				document.getElementById("cal_"+col+"."+row).innerHTML = "<p align=\"center\"><b><a onclick=\"callCreatePopup(document.getElementById('cal_" + col + "." + row + "'),'" + current_year + "-" + current_month + "-" + day + "')\";\">"+day+"</a></b></p>";
				var temp_day = document.getElementById("cal_"+col+"."+row);
				temp_day.style.backgroundColor = "#CCFFCC";
			}
			else
			{
				document.getElementById("cal_"+col+"."+row).innerHTML = "<p align=\"center\">"+day+"</p>";
				if(currentDate.getDate() == day)
				{
					var temp_day = document.getElementById("cal_"+col+"."+row);
					temp_day.style.backgroundColor = "#c0c0c0";
				}
			}
	   }
	   day = null;
	   col = null;
	   row = null;
	   events = null;
	   temp_day = null;
	   dates = null;
	   node = null;
	}
	catch(err)
	{
		add_to_console("Error returned when calling calendar update, retrying");
		callUpdateCalendar();
	}
  }
  else 
  {
   // Tell the reader that there was error requesting data
   alert("ERRRROR: YOU BROKE THE INTERWEB");
  }
 }
}

function display_cal_window()
{
	window.open(CalendarBackendBase, "","");
}